<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-button type="danger" @click="dialogFormVisible = true;">新增</el-button>
          <el-button type="success" @click="rangeAdd">随机生成</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column label="小说名称">
          <template slot-scope="scope">{{scope.row.booksVo.booksName}}</template>
        </el-table-column>
        <el-table-column label="作者">
          <template slot-scope="scope">{{scope.row.booksVo.author}}</template>
        </el-table-column>
        <el-table-column label="适合读者">
          <template slot-scope="scope">
            <el-tag
              type="success"
            >{{scope.row.booksVo.readerSex == config.blockType.man ? '男生' : '女生'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image style="width: 60px;" :src="scope.row.booksVo.booksCover" fit="contain"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="关联管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="小说名称" prop="booksIds">
            <el-select
              v-model="form.booksIds"
              multiple
              filterable
              remote
              reserve-keyword
              placeholder="请输入小说名称"
              :remote-method="queryByBooksName"
            >
              <el-option
                v-for="item in booksList"
                :key="item.id"
                :label="item.booksName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getBlockJoinBooksList,
  saveBlockJoinBooks,
  deleteBlockJoinBooks,
  range
} from "api/BlockJoinBooksApi";

import { getBooksAllList } from "api/BooksApi";

import { config } from "assets/js/Data";

export default {
  name: "BlockJoinBooksList",
  data() {
    return {
      blocksId: 0,
      dialogFormVisible: false,
      form: {
        booksIds: []
      },
      rules: {
        booksIds: {required: true, message: "请选择小说"}
      },
      tableData: [],
      listNum: 0,
      search: {
        currentIndex: 1
      },
      config: config,
      booksList: []
    };
  },
  methods: {
    queryByBooksName(query) {
      if (query != "") {
        getBooksAllList ({booksName: query})
          .then(res => {
            if(res) {
              this.booksList = res
            }
          });
      } else this.booksList = []
    },
    getBlockJoinBooksList() {
      getBlockJoinBooksList(this.search).then(res => {
        if (res) {
          this.tableData = res.datas;
          this.listNum = res.pagination.totalCount;
        }
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getBlockJoinBooksList();
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteBlockJoinBooks({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getBlockJoinBooksList();
          }
        });
      });
    },
    onSubmit() {
      this.form.blockId = this.search.blockId;

      this.$refs.form.validate(valid => {
        if (valid) {
          //保存
          saveBlockJoinBooks (this.form)
            .then(res => {
              if(res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getBlockJoinBooksList();
              }
            })
        }
      });
    },
    rangeAdd() {
      range ({
        blockId: this.search.blockId
      }).then(res => {
        if(res) {
          this.getBlockJoinBooksList();
        }
      })
    }
  },
  created() {
    this.search.blockId = this.$route.params.blocksId;
    this.getBlockJoinBooksList();
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
      }
    }
  }
};
</script>
<style lang="stylus" scoped></style>
